package com.wyw.netty.learning.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

/**
 * 文件介绍
 *
 * @Author： Mr Wu （yewen.wu.china.gmail.com）
 * @Date: 2021/2/3 16:49
 */
public class NIOEchoServer {
    public static void main(String[] args) throws IOException {
        //创建一个Selector
        Selector selector = Selector.open();
        //创建一个ServerSocketChannel
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        //绑定8082端口
        serverSocketChannel.bind(new InetSocketAddress(8082));
        //设置为非阻塞模式
        serverSocketChannel.configureBlocking(false);
        //将Channel注册到selector上，并注册Accept事件
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        System.out.println("server start");
        while(true){
            //阻塞再select上（第一阶段阻塞）
            selector.select();

            // 如果使用的是select(timeout)或selectNow()需要判断返回值是否大于0

            //有就绪的Channel
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            //遍历selectKeys
            Iterator<SelectionKey> iterator = selectionKeys.iterator();
            while(iterator.hasNext()){
                SelectionKey selectionKey = iterator.next();
                //如果是accept事件
                if(selectionKey.isAcceptable()){
                    //强转为ServerSocketChannel
                    ServerSocketChannel scc = (ServerSocketChannel) selectionKey.channel();
                    SocketChannel socketChannel = scc.accept();
                    System.out.println("accept new conn: " + socketChannel.getRemoteAddress());
                    socketChannel.configureBlocking(false);
                    //将SocketChannel注册到Selector上，并注册读事件
                    socketChannel.register(selector, SelectionKey.OP_READ);
                } else if(selectionKey.isReadable()){
                    //如果是读事件
                    //强转为SocketChannel
                    SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
                    //创建BUFFER读取数据
                    ByteBuffer buffer = ByteBuffer.allocate(1024);
                    //将数据读入buffer用于读取数据（第二阶段阻塞）
                    int length = socketChannel.read(buffer);
                    if(length > 0){
                        buffer.flip();
                        byte[] bytes = new byte[buffer.remaining()];
                        //将数据读入byte数组
                        buffer.get(bytes);

                        // 换行符会跟着消息一起传过来
                        String content = new String(bytes, "UTF-8").replace("\r\n", "");
                        System.out.println("receive msg: " + content);
                    }
                }
                iterator.remove();
            }

        }

    }
}
